<template>
  <div v-theme:column="'wide'" id="show-blogs">
    <h1>博客总览</h1>
    <input type="text" placeholder="请输入标题" v-model="search"/>
    <div class="single-blog" v-for="b in filteredBlogs">
      <router-link v-bind:to="/blog/+b.id">
        <h2 v-rainbow>{{b.title }}</h2>
      </router-link>
      <router-link v-bind:to="'/'">
        <button id="del_btn" v-on:click="del(b.id)">删除</button>
      </router-link>
      <router-link v-bind:to="'/update'">
        <button id="update_btn" v-on:click="update(b.id)">修改</button>
      </router-link>
      <article>
        {{b.content  }}
      </article>
    </div>
  </div>
</template>

<script>
    export default {
        name: "ShowBlog",
      data(){
          return{
            blogs:[],
            search:"",
          }
      },
      created() {
          this.axios.get("/api/blog/list")
            .then((data)=>{
              this.blogs = data.data;
            })
      },
      computed: {
        filteredBlogs:function () {
          return this.blogs.filter((blog)=>{
            return   blog.title.match(this.search);
          })
        }
      },
      methods:{
          del:function (id) {
            if(confirm("确认删除吗:")){
              this.axios.get("/api/blog/del/"+id)
                .then((data)=>{
                  console.log(data);
                  if (data.data=="1"){
                    alert("OK");
                    location.reload();
                  } else{
                    alert("No Ok");
                  }
                })
            }
          },
        update:function (bid) {
          this.$router.push({name:"updateBlog",params:{id:bid}});
        }
      }

    }
</script>

<style scoped>
  #show-blogs {
    max-width: 800px;
    margin: 0 auto;
  }
  .single-blog{
    padding: 20px;
    margin: 20px 0;
    box-sizing: border-box;
    background-color: #eee;
    position: relative;
  }

  input[type=text]{
    width:100%;
    height: 40px;
    border:1px solid #ccc;
  }
  #del_btn{
    width: 50px;
    line-height: 30px;
    position: absolute;
    background-color: white;
    right: 10px;
    top: 50px;
  }
  #update_btn{
    width: 50px;
    line-height: 30px;
    position: absolute;
    background-color: white;
    right: 80px;
    top: 50px;
  }
</style>
